﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using System.Threading.Tasks;
using Lucene.Net.Documents;
using System.Threading;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Index;

namespace Lucandra.Net.Tests
{
    [TestClass]
    public class MultiThreadedTests
    {
        private static string indexName;
        private static Analyzer analyzer;
        private static int count;

        static MultiThreadedTests()
        {
            indexName = "Test Index";
            analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            count = 25;
        }

        [TestMethod]
        public void TestMultiThreadedWrite()
        {
            var writer = new LucandraWriter(indexName);

            Parallel.For(0, count, (i) =>
            {
                var doc = new Document();
                doc.Add(new Field("id", "number" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                writer.AddDocument(doc, analyzer);
            });

            Thread.Sleep(5000);

            var reader = new LucandraReader(indexName);
            var searcher = new LucandraSearcher(reader);

            var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "id", analyzer);
            var query = qp.Parse("+id:number*");

            var docs = searcher.Search(query, count + 1);

            Assert.AreEqual(count, docs.totalHits);
        }

        [TestMethod]
        public void TestMultiThreadedSearch()
        {
            var reader = new LucandraReader(indexName);
            var searcher = new LucandraSearcher(reader);

            Parallel.For(0, count, (i) =>
                {
                    var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "id", analyzer);
                    var query = qp.Parse("+id:number" + i);
                    var docs = searcher.Search(query, 2);
                    Assert.AreEqual(1, docs.totalHits);
                });
        }

        [TestMethod]
        public void TestMultiThreadedDelete()
        {
            var writer = new LucandraWriter(indexName);

            Parallel.For(0, count, (i) =>
                {
                    var qp1 = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "id", analyzer);
                    var query1 = qp1.Parse("+id:number" + i);
                    writer.DeleteDocuments(query1);
                });

            Thread.Sleep(5000);
           
            var reader = new LucandraReader(indexName);
            var searcher = new LucandraSearcher(reader);

            var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "id", analyzer);
            var query = qp.Parse("+id:number*");

            var docs = searcher.Search(query, count + 1);

            Assert.AreEqual(0, docs.totalHits);
        }
    }
}
